//
//  RWViewController.m
//  RWReactivePlayground
//
//  Created by Colin Eberhardt on 18/12/2013.
//  Copyright (c) 2013 Colin Eberhardt. All rights reserved.
//

#import "RWViewController.h"
#import "RWDummySignInService.h"
#import <ReactiveCocoa/ReactiveCocoa.h>

@interface RWViewController ()

@property (weak, nonatomic) IBOutlet UITextField *usernameTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@property (weak, nonatomic) IBOutlet UIButton *signInButton;
@property (weak, nonatomic) IBOutlet UILabel *signInFailureText;

@property (strong, nonatomic) RWDummySignInService *signInService;

@end

@implementation RWViewController

- (RACSignal *)signInSignal {
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        [self.signInService signInWithUsername:self.usernameTextField.text password:self.passwordTextField.text complete:^(BOOL success) {
            [subscriber sendNext:@(success)];
            [subscriber sendCompleted];
        }];
        return nil;
    }];
}
- (void)test {
    self.signInService = [RWDummySignInService new];
    
    // initially hide the failure message
    self.signInFailureText.hidden = NO;
    
    //    [self.usernameTextField.rac_textSignal subscribeNext:^(id x) {
    //        LxDBAnyVar(x);
    //    }];
    //    [[self.usernameTextField.rac_textSignal filter:^BOOL(id value) {
    //        NSString *text = value;
    //        return text.length > 3;
    //    }] subscribeNext:^(id x) {
    //        // 满足上面添加，textview 文字长度大于三，才会监听
    //        LxDBAnyVar(x);
    //    }];
    
    
    //    RACSignal *usernameSingnal = self.usernameTextField.rac_textSignal;
    //    RACSignal *filterdUserName = [usernameSingnal filter:^BOOL(id value) {
    //        NSString *text = value;
    //        return text.length > 3;
    //    }];
    //    [filterdUserName subscribeNext:^(id x) {
    //        LxDBAnyVar(x);
    //    }];
    
    //    [[[self.usernameTextField.rac_textSignal map:^id(NSString *text) {
    //        // 管道筛选属性
    //        return @(text.length);  // 确定后筛选文字长度111
    //    }] filter:^BOOL(NSNumber *length) {
    //        // 获取文字长度
    //        return length.integerValue > 3; // 长度大于3
    //    }] subscribeNext:^(id x) {
    //        // 最终获取文字长度
    //        LxDBAnyVar(x);
    //    }];
    
    
    RACSignal *validUsernameSignal = [self.usernameTextField.rac_textSignal map:^id(NSString *userName) {
        return @(userName.length >= 3) ; // 用户名长度大于3是有效信号
    }];
    RACSignal *validPassWord = [self.passwordTextField.rac_textSignal map:^id(NSString *passWord) {
        return @(passWord.length >= 6);  // 密码长度大于6是有效信号
    }];
    //      [[validPassWord map:^id(NSNumber *value) {
    //          return value.boolValue ? [UIColor clearColor] : [UIColor yellowColor];
    //      }] subscribeNext:^(UIColor *color) {
    //          self.passwordTextField.backgroundColor = color;
    //      }];
    RAC(self.usernameTextField,backgroundColor) = [validUsernameSignal map:^id(NSNumber *value) {
        return value.boolValue ? [UIColor clearColor] : [UIColor yellowColor];
    }];
    RAC(self.passwordTextField,backgroundColor) = [validPassWord map:^id(NSNumber *value) {
        return value.boolValue ? [UIColor clearColor] : [UIColor yellowColor];
    }];
    
    RACSignal *signUpActiveSignal = [RACSignal combineLatest:@[validUsernameSignal,validPassWord] reduce:^id(NSNumber *userName,NSNumber *pwd){
        return @(userName.boolValue && pwd.boolValue);
    }];
    
    [signUpActiveSignal subscribeNext:^(NSNumber *x) {
        self.signInButton.enabled = x.boolValue;
    }];
    
    //    [[[self.signInButton rac_signalForControlEvents:UIControlEventTouchUpInside] map:^id(id value) { // 筛选
    //        return [self signInSignal]; // 根据返回值，看能否进入下一步
    //    }] subscribeNext:^(NSNumber *success) {
    //        if (success.boolValue) {
    //            SHOW_ALERT(@"登陆成功-1");
    //        }
    //    }];
    [[[[self.signInButton rac_signalForControlEvents:UIControlEventTouchUpInside] doNext:^(id x) {
        // 这里是点击按钮后还可以做一些事情
        self.signInButton.enabled = NO;
        self.signInFailureText.hidden = YES;
    }] flattenMap:^id(id value) {
        return [self signInSignal];
    }] subscribeNext:^(NSNumber *success) {
        if (success.boolValue) {
            SHOW_ALERT(@"登陆成功-2");
        }
        self.signInButton.enabled = YES;
        self.signInFailureText.hidden = success;
    }];
    
}
- (void)viewDidLoad {
  [super viewDidLoad];
    [self test];
}
@end
